package com.zbec.mdrs.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public static String DATE_FORMAT = "dd MMM yyyy";
    public static String DATE_FORMAT_ddMMMyyy = "dd-MMM-yyyy";
    public static String DATE_FORMAT_ddMMyyyy = "ddMMyyyy";
    public static String DATE_FORMAT_YYYYMMdd = "yyyyMMdd";
    public static String DATE_FORMAT_YYYYMM = "yyyyMM";
    public static String TIMESTAMP_FORMAT = "dd MMM yyyy HH:mm:ss";
    public static String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";


    public static Date parseDate(String strDate, String pattern) throws ParseException {
        if (strDate == null || strDate.length() == 0)
            return null;
        SimpleDateFormat formater = new SimpleDateFormat(pattern);
        Date result = formater.parse(strDate);
        return result;
    }

    /**
     * Returns a date object from input string indicating year, month and day
     *
     * @param year  Year Indicator
     * @param month Month indicator, 1=jan 2=feb ...
     * @param day   Date indicator eg: any day from 1...31.
     * @return date java.util.Date object in millisecond.
     * @since 15/05/2000
     */
    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day, 0, 0, 0);

        return cal.getTime();
    }

    public static Date getLastDayOfThisMonth(Date currentDate) {
        Calendar temp = Calendar.getInstance();
        temp.setTime(currentDate);
        int max = temp.getActualMaximum(Calendar.DAY_OF_MONTH);
        temp.set(Calendar.DAY_OF_MONTH, max);
        return getDateWithYearMonthDay(temp.getTime());
    }


    public static String convertDateToString(String format, Date aDate) {
        return getDateTime(format, aDate);
    }

    public static Date getDateWithYearMonthDay(Date date){
        if(date == null){
            return null;
        }
        String dateStr = DateUtils.parseDate(date, DateUtils.DATE_FORMAT_ddMMyyyy);
        return DateUtils.toDate(dateStr, DateUtils.DATE_FORMAT_ddMMyyyy);
    }

    /**
     * Parses Date object to formatted string
     *
     * @param date      date to be converted
     * @param formatStr Date/Time pattern. Example: ddMMyyyy or HHmmss or any
     *                  other patterns
     * @return String in required format  Format  : dd = Day MM = Month yyyy =
     *         Year HH = Hour mm = Minute ss = Second All format same as
     *         SimpleDateFormat. Null is returned if the date object is null.
     * @since 22/03/2001
     */
    public static String parseDate(Date date, String formatStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr,Locale.ENGLISH);

        if (date == null) {
            return null;
        } else {
            return dateFormat.format(date);
        }
    }

    /**
     * Converts the specified date-time string to Date object based on the
     * specified date-time format. <CODE>null</CODE> is returned if the
     * specified date is invalid.
     *
     * @param strDateTime    The date string in this format 'ddMMyyyy  HHmm'.
     * @param dateTimeFormat The date pattern in this format 'ddMMyyyy  HHmm'
     * @return the Date representation.  Returns null if the date object or the
     *         strDateTime or the dateTimeFormat is null.
     */
    public static Date toDate(String strDateTime, String dateTimeFormat) {
        if ((strDateTime == null) || (strDateTime.length() == 0) ||
                (dateTimeFormat == null) || (dateTimeFormat.length() == 0)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(dateTimeFormat);
        Date date = dateFormat.parse(strDateTime, new ParsePosition(0));

        if (date == null) {
            return null;
        }

        String dateStr = parseDate(date, dateTimeFormat);

        if (!strDateTime.equals(dateStr)) {
            return null;
        }

        return date;
    }


    /**
     * This method generates a string representation of a date's date/time
     * in the format you specify on input
     *
     * @param aMask the date pattern the string is in
     * @param aDate a date object
     * @return a formatted string representation of the date
     * @see java.text.SimpleDateFormat
     */
    public static String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df;
        String returnValue = "";
        Locale locale = Locale.UK;
        if (aDate == null) {
            throw new IllegalArgumentException("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask,locale);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }
    
    /**
     * Adds the specified number of Days to the specified Date object
     * To subtract the specified number of Days to the specified Date object, juz use a negative number
     * Example: DateUtil.addDaysToDate(date, -5) ==  subtracting 5 days from the specified date.
     *
     * @param date    Date to be add
     * @param numDays Number of days to add
     * @return date  Added Date
     */
    public static Date addDaysToDate(Date date, int numDays) {
        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, numDays);

        return c.getTime();
    }
    
    /**
     * The method will compares 2 dates (excluding the HH MM SS)
     *
     * @param date1 1st date parameter
     * @param date2 2nd date parameter
     * @return returns -1 if 1st date parameter is earlier than 2nd date
     *         parameter retuns 0 if both dates parameter is the same day
     *         retuns 1 if 1st date parameter is later than 2nd date parameter
     */
    public static int compareDates(Date date1, Date date2) {
        if ((date1 == null) && (date2 == null)) {
            return 0;
        }

        if (date1 == null) {
            return -1;
        }

        if (date2 == null) {
            return 1;
        }

        String strFormat = "yyyyMMdd";
        SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);

        int intDate1 = Integer.parseInt(dateFormat.format(date1));
        int intDate2 = Integer.parseInt(dateFormat.format(date2));

        if (intDate1 == intDate2) {
            return 0;
        }

        if (intDate1 > intDate2) {
            return 1;
        }

        return -1;
    }
}